package com.molyfun.parents.activity.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.MainActivity;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.push.PushMsgCacheCenter;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.RegThirdPartyResponse;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.VerificationInfomationUtils;
import com.molyfun.parents.view.SMSCountDownTimer;
import com.molyfun.parents.view.SinglineClearEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.smssdk.SMSSDK;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CompletedInformationActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;
    @InjectView(R.id.phone_num)
    SinglineClearEditText mPhoneNum;
    @InjectView(R.id.code)
    SinglineClearEditText mCode;
    @InjectView(R.id.get_code)
    TextView mGetCode;
    @InjectView(R.id.layout_newPwd)
    LinearLayout mLayoutNewPwd;
    @InjectView(R.id.activity_login_pwd)
    SinglineClearEditText mLoginPwd;
    @InjectView(R.id.commit)
    Button mCommit;
    @InjectView(R.id.sub_title)
    TextView mSubTitle;

    SMSCountDownTimer smsCountDownTimer;
    private String nonce;
    private String timesptamp;
    private String signature;
    private String openid;
    private String WXTYPE = "2";

    public static void startyActivity(Context context, String nonce, String timesptamp, String signature, String openid) {
        Intent intent = new Intent();
        intent.putExtra("nonce", nonce);
        intent.putExtra("timesptamp", timesptamp);
        intent.putExtra("signature", signature);
        intent.putExtra("openid", openid);
        intent.setClass(context, CompletedInformationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mBaseTitle.setText("完善个人信息");
        mSubTitle.setText("请完善信息继续浏览");

        nonce = getIntent().getStringExtra("nonce");
        timesptamp = getIntent().getStringExtra("timesptamp");
        signature = getIntent().getStringExtra("signature");
        openid = getIntent().getStringExtra("openid");

        mBack.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        mCommit.setEnabled(true);
        mGetCode.setOnClickListener(this);
        mLayoutNewPwd.setVisibility(View.INVISIBLE);
        smsCountDownTimer = new SMSCountDownTimer(mGetCode, 60000, 1000);

    }

    @Override
    protected void onDestroy() {
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.commit:
                resetPwd(mPhoneNum.getString(), mLoginPwd.getString(), mCode.getString());
                break;
            case R.id.get_code:
                if (TextUtils.isEmpty(mPhoneNum.getString())) {
                    toast("请输入手机号");
                    return;
                }
                if(!VerificationInfomationUtils.isMobileNO(mPhoneNum.getString().trim())){
                    toast("请输入正确手机号");
                    return;
                }
                //TODO 请求接口
                checkPhone();

                Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().getSmsCode(mPhoneNum.getString(), "regthirdparty");
                call.enqueue(new Callback<BaseResponse>() {
                    @Override
                    public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                        if (response.body() != null) {
                            if (response.body().getCode() == 200) {
                                toast("验证码发送成功");
                            } else {
                                toast(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResponse> call, Throwable t) {
                        toast("请求失败");
                    }
                });

                smsCountDownTimer.start();
                break;
            default:
                break;
        }
    }


    private void checkPhone() {
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().checkPhone(mPhoneNum.getString(), nonce, timesptamp, signature);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if (response.body() != null && response.body().getMsg() != null) {
                    if (response.body().getCode() == 200) {
                        mLayoutNewPwd.setVisibility(View.INVISIBLE);
                    } else if (response.body().getCode() == 1030) {
                        mLayoutNewPwd.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {

            }
        });
    }

    private void resetPwd(String phone, String pwd, String captchacode) {
        Call<RegThirdPartyResponse> call = MFPApplication.getInstance().getRestClient().registerThirdAccound(phone, captchacode, openid, pwd, WXTYPE);
        call.enqueue(new Callback<RegThirdPartyResponse>() {
            @Override
            public void onResponse(Call<RegThirdPartyResponse> call, Response<RegThirdPartyResponse> response) {
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                       if(response.body().getData() != null) {
                           PreferencesStorageUtil.setToken(CompletedInformationActivity.this, response.body().getData().getToken());
                           PreferencesStorageUtil.setUserBean(CompletedInformationActivity.this, GsonUtil.toJson(response.body().getData()));
                           PreferencesStorageUtil.setIMUserName(CompletedInformationActivity.this, response.body().getData().getImusername());
                           PreferencesStorageUtil.setIMUserPwd(CompletedInformationActivity.this, response.body().getData().getImpassword());
                           PreferencesStorageUtil.setPhoneNum(CompletedInformationActivity.this, response.body().getData().getPhone());
                           PreferencesStorageUtil.setAddress(CompletedInformationActivity.this, response.body().getData().getAddress());
                           PreferencesStorageUtil.setEmail(CompletedInformationActivity.this, response.body().getData().getEmail());
                           PreferencesStorageUtil.setStudentId(CompletedInformationActivity.this, response.body().getData().getStudentid());
                           PreferencesStorageUtil.setThirdBindingStatu(CompletedInformationActivity.this, response.body().getData().getThirdlist().get(0).getStatus());
                           PreferencesStorageUtil.setThirdAccount(CompletedInformationActivity.this, response.body().getData().getThirdlist().get(0).getUsername());
                           PreferencesStorageUtil.setThirdOpenId(CompletedInformationActivity.this, response.body().getData().getThirdlist().get(0).getOpenid());
                           if (TextUtils.isEmpty(response.body().getData().getStudentid())) {
                               PreferencesStorageUtil.setUserEnter(CompletedInformationActivity.this, false);
                           } else {
                               PreferencesStorageUtil.setUserEnter(CompletedInformationActivity.this, true);
                               PreferencesStorageUtil.setOriganOutShowSplash(MFPApplication.getInstance(), false);
                           }
                           MFPApplication.getInstance().refreshRestClient(response.body().getData().getToken());
                           PushMsgCacheCenter.getInstance().setmCurrentUnreadCount(response.body().getData().getMessageunreadcount());

                           Intent toMainIntent = new Intent(CompletedInformationActivity.this, MainActivity.class);
                           CompletedInformationActivity.this.startActivity(toMainIntent);
                           finish();

                       }
                    } else {
                        toast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<RegThirdPartyResponse> call, Throwable t) {
                toast("失败");
            }
        });
    }

}
